<template>
  <div class="emergency_box">
    <div class="emergency_top">
      <div class="emergency_title">设备紧急程度分布</div>
      <div class="emergency_month">
        <div :class="this.searchDateNumber == 2? 'emergency_ThisMarch1':'emergency_March'" @click="getMarchList"><span>近三个月</span></div>
        <div :class="this.searchDateNumber == 1? 'emergency_ThisMarch1':'emergency_LastMarch'" @click="getLastMarchList"><span>上月</span></div>
        <div :class="this.searchDateNumber == 0? 'emergency_ThisMarch2':'emergency_ThisMarch'" @click="getList"><span>本月</span></div>
      </div>
    </div>
    <div v-if="tableData&&tableData.length>0" class="echarts_container">
      <div id="urgency" class="col-md-12  col-sm-12 col-xs-12" style="height: 100%;" />
    </div>
    <div v-else class="echarts_noData">
      <svg-icon icon-class="暂无数据" />
    </div>
  </div>
</template>

<script>
import { deviceEmergencyDegreeDistribution } from '@/api/equipmentManage/equipmentReport/dataBoard'
export default {
  name: 'EmergencyDegreeDistribution',
  data() {
    return {
      msg: 'fafa',
      tableData: [1],
      colorData: ['1'],
      searchDateNumber: 0,
      colorArray: [
        { name: '轻微', color: '#6CF72D' },
        { name: '重要', color: '#FFCC00' },
        { name: '紧急', color: '#FF8385' }
      ]
    }
  },
  mounted() {
    // this.drawLine()
    this.getList()
  },
  methods: {
    getData(res) {
      const arr = []
      const colors = []
      this.colorArray.forEach(item => {
        if (res.data.some(item2 => item.name == item2.emergencyDegree)) {
          const matchedItem = res.data.filter(item3 => item3.emergencyDegree == item.name)[0]
          if (matchedItem.number && matchedItem.number !== '0') {
            arr.push({
              value: matchedItem.number,
              name: matchedItem.emergencyDegree
            })
            colors.push(item.color)
          }
        }
      })

      this.tableData = arr
      this.colorData = colors
      this.$nextTick(() => {
        this.drawLine()
      })
    },
    getList() {
      this.searchDateNumber = 0
      deviceEmergencyDegreeDistribution({
        searchDateNumber: 0,
        isContinuous: 0
      }).then(res => {
        this.getData(res)
      })
    },
    getLastMarchList() {
      this.searchDateNumber = 1
      deviceEmergencyDegreeDistribution({
        searchDateNumber: 1,
        isContinuous: 0
      }).then(res => {
        this.getData(res)
      })
    },
    getMarchList() {
      this.searchDateNumber = 2
      deviceEmergencyDegreeDistribution({
        searchDateNumber: 2,
        isContinuous: 1
      }).then(res => {
        this.getData(res)
      })
    },
    drawLine() {
      const myChart = this.$echarts.init(document.getElementById('urgency'))
      myChart.setOption({
        tooltip: {
          trigger: 'item'
        },
        // 右边信息面板
        legend: {
          orient: 'vertical',
          right: '10%',
          top: 'middle',
          itemHeight: 15,
          itemWidth: 45,
          itemGap: 30,
          textStyle: {
            color: '#999999',
            fontSize: 18,
            padding: [0, 15]
          }
        },
        // 设置饼状图每个颜色块的颜色
        color: this.colorData,
        // 牵引线
        series: [
          {
            type: 'pie',
            radius: ['18%', '60%'],
            center: ['32%', '50%'],
            avoidLabelOverlap: true,
            silent: true,
            label: {
              show: true,
              position: 'outer',
              width: 0,
              height: 0,
              lineHeight: 0,
              backgroundColor: 'auto',
              padding: [2, 1, 2, -1],
              borderRadius: 2,
              distanceToLabelLine: 0,
              // 格式化牵引线上文字
              formatter: '{c|{c}}',
              align: 'center',
              rich: {
                top: {
                  verticalAlign: 'bottom',
                  padding: [5, 10, 0, 10],
                  align: 'center',
                  // 牵引线上文字大小
                  fontSize: 12
                },
                bottom: {
                  padding: [0, 10, 5, 10],
                  verticalAlign: 'top',
                  align: 'center'
                },
                c: {
                  color: '#666666'
                }
              }
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: true, // 数据标签引导线
              // 牵引线长度
              length: 10,
              length2: 20,
              lineStyle: {
                width: 1,
                type: 'solid',
                color: '#979797'
              }
            },
            data: this.tableData
          }
        ]
      })
    }
  }
}
</script>

<style lang="less" scoped>
.emergency_box{
  width: 100%;
  height: calc(100% - 50px);
}
  .emergency_top{
    display: flex;
    justify-content: space-between;
    .emergency_title{
      font-size: 24px;
      line-height: 34px;
      font-weight: bold;
      letter-spacing: -0.21px;
      color: #333333;
      padding-top: 16px;
      padding-left: 40px;
    }
    .emergency_month{
      display: flex;
      font-size: 18px;
      font-weight: normal;
      line-height: 25px;
      letter-spacing: -0.16px;
      color: #999999;
      padding-top: 22px;
      padding-right: 40px;
      cursor: pointer;
      .emergency_ThisMarch1{
        font-size: 18px;
        font-weight: normal;
        line-height: 25px;
        letter-spacing: -0.16px;
        color: #387BFC;
        padding-right: 30px;
      }
      .emergency_ThisMarch2{
        font-size: 18px;
        font-weight: normal;
        line-height: 25px;
        letter-spacing: -0.16px;
        color: #387BFC;
      }
      .emergency_March{
        padding-right: 30px;
      }
      .emergency_LastMarch{
        padding-right: 30px;
      }
    }
  }

.echarts_container{
  width: 100%;
  height: 100%;
}
</style>

